﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Department.App_Code
{
    public class CallStoredProcedure
    {
        private static string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["DepartmentConnectionString"].ConnectionString;

        // run a stored procedure that takes one parameter and returns search results
        public static DataTable StoredProcSearch(string param, int departmentId)
        {
            SqlConnection conn = new SqlConnection(strConnString);
            DataTable dt = new DataTable();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("sp_site_search", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@SearchText", param));
                cmd.Parameters.Add(new SqlParameter("@aFacDeptId", departmentId));
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(dt);
                }
                return dt;
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }

        public static DataTable FilterNews(string title, string date, string text, int depID)
        {
            SqlConnection conn = new SqlConnection(strConnString);
            DataTable dt = new DataTable();
            if (title.Equals(""))
            {
                title = null;
            }
            if (date.Equals(""))
            {
                date = null;
            }
            if (text.Equals(""))
            {
                text = null;
            }
            try
            {

                conn.Open();
                SqlCommand cmd = new SqlCommand("sp_SearchInNews", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Name", title));
                cmd.Parameters.Add(new SqlParameter("@Text", text));
                cmd.Parameters.Add(new SqlParameter("@Date", date));
                cmd.Parameters.Add(new SqlParameter("@DepID", depID));

                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(dt);

                }

                return dt;

            }
            catch (SqlException ex)
            {
                ex.ToString();
                return dt;
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }
        public static DataTable FilterContent(string title, string date, string text, string tableToSearch)
        {
            SqlConnection conn = new SqlConnection(strConnString);
            DataTable dt = new DataTable();
            if (title.Equals(""))
            {
                title = null;
            }
            if (date.Equals(""))
            {
                date = null;
            }
            if (text.Equals(""))
            {
                text = null;
            }
            try
            {

                conn.Open();
                SqlCommand cmd = new SqlCommand("sp_SearchInContent", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Name", title));
                cmd.Parameters.Add(new SqlParameter("@Text", text));
                cmd.Parameters.Add(new SqlParameter("@Date", date));
                cmd.Parameters.Add(new SqlParameter("@TableToSearch", tableToSearch));

                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(dt);

                }

                return dt;

            }
            catch (SqlException ex)
            {
                ex.ToString();
                return dt;
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }
        public static DataTable FilterDocuments(string title, string date, string text)
        {
            SqlConnection conn = new SqlConnection(strConnString);
            DataTable dt = new DataTable();
            if (title.Equals(""))
            {
                title = null;
            }
            if (date.Equals(""))
            {
                date = null;
            }
            if (text.Equals(""))
            {
                text = null;
            }
            
            try
            {

                conn.Open();
                SqlCommand cmd = new SqlCommand("sp_SearchForDocuments", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Name", title));
                cmd.Parameters.Add(new SqlParameter("@Text", text));
                cmd.Parameters.Add(new SqlParameter("@Date", date));


                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(dt);

                }

                return dt;

            }
            catch (SqlException ex)
            {
                ex.ToString();
                return dt;
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }

        public static DataTable FilterEvents(string title, string date, string text, int depID)
        {
            SqlConnection conn = new SqlConnection(strConnString);
            DataTable dt = new DataTable();

            // values are not passed as NULL to the stored procedure but as empty strings thus no results are shown 
            if (title.Equals(""))
            {
                title = null;
            }
            if (date.Equals(""))
            {
                date = null;
            }
            if (text.Equals(""))
            {
                text = null;
            }
            try
            {

                conn.Open();
                SqlCommand cmd = new SqlCommand("sp_SearchInEvents", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Name", title));
                cmd.Parameters.Add(new SqlParameter("@Text", text));
                cmd.Parameters.Add(new SqlParameter("@Date", date));
                cmd.Parameters.Add(new SqlParameter("@DepID", depID));

                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(dt);

                }

                return dt;

            }
            catch (SqlException)
            {
                return dt;
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }
    }
}